//
//  UIButtonVertical.m
//  SecondPackage
//
//  Created by 韩艳锋 on 2019/7/2.
//  Copyright © 2019 韩艳锋. All rights reserved.
//

#import "UIButtonVertical.h"

@implementation UIButtonVertical

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

- (instancetype)init
{
    self = [super init];
    if (self) {
        self.contentVerticalAlignment = UIControlContentVerticalAlignmentTop;
        self.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
        self.spaceImageText = 10;
    }
    return self;
}
-(void)layoutSubviews {
    [super layoutSubviews];
    UIImageView * imageView = self.imageView;
    UILabel * label = self.titleLabel;
    if (imageView && label) {
        CGFloat imageTop = (self.frame.size.height-imageView.frame.size.height-label.frame.size.height)/2;
        CGFloat titleTop = imageTop+imageView.frame.size.height;
        CGFloat imageLeft = (self.frame.size.width-imageView.frame.size.width)/2;
        CGFloat titleLeft = (self.frame.size.width-label.frame.size.width)/2-imageView.frame.size.width;
        self.imageEdgeInsets = UIEdgeInsetsMake(imageTop - self.spaceImageText / 2, imageLeft, 0, 0);
        self.titleEdgeInsets = UIEdgeInsetsMake(titleTop + self.spaceImageText / 2, titleLeft, 0, 0);
    }
}

@end
